package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.PKYKMapper;
import com.zhongyu.mapper.StoreOperMapper;
import com.zhongyu.pojo.Panyingpankui;
import com.zhongyu.pojo.StoreOper;
import com.zhongyu.pojo.User;
import com.zhongyu.service.PKYKService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.PkykVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service("pKYKService")
public class PKYKServiceImpl extends ServiceImpl<PKYKMapper, Panyingpankui> implements PKYKService {

    @Resource
    private PKYKMapper pkykMapper;
    @Resource
    private Panyingpankui panyingpankui;

    @Resource
    private StoreOper storeOper;
    @Resource
    private StoreOperMapper storeOperMapper;
    @Override
    public PageResult searchAll(Integer pageNo, Integer pageSize, PkykVo pkykVo) {
        LambdaQueryWrapper<Panyingpankui> wrapper= Wrappers.<Panyingpankui>lambdaQuery();
        wrapper  .eq(pkykVo.getType()!=null, Panyingpankui::getType,pkykVo.getType())
                .ge(pkykVo.getStartDate()!=null, Panyingpankui::getCreatetime,pkykVo.getStartDate())
                .le(pkykVo.getEndDate()!=null, Panyingpankui::getCreatetime,pkykVo.getEndDate())
                .ge(pkykVo.getStartDate2()!=null, Panyingpankui::getChecktime,pkykVo.getStartDate2())
                .le(pkykVo.getEndDate2()!=null, Panyingpankui::getChecktime,pkykVo.getEndDate2());
        Page page=new Page(pageNo,pageSize);
        List<Panyingpankui> list=pkykMapper.findAll(page,wrapper);
        return new PageResult(list,page.getTotal());
    }

    @Override
    public void add(Panyingpankui panyingpankui, HttpSession session) {

        User loginUser= (User) session.getAttribute("loginUser");
        if (loginUser!=null){
            panyingpankui.setCreater(loginUser.getId());
        }
       panyingpankui.setState(Panyingpankui.STATE_CREATE);
        panyingpankui.setCreatetime(LocalDateTime.now());

        pkykMapper.insert(panyingpankui);


    }

    @Override
    public PageResult searchAll(Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<Panyingpankui> wrapper= Wrappers.<Panyingpankui>lambdaQuery();
        Page page=new Page(pageNo,pageSize);
        List<Panyingpankui> list=pkykMapper.find(page,wrapper);
        return new PageResult(list,page.getTotal());
    }

    @Override
    public Panyingpankui searchAll(Integer id) {
        return pkykMapper.searchAll(id);
    }

    @Override
    public void update(Integer state, LocalDateTime checktime, Integer id, Integer shenhe, HttpSession session, Panyingpankui panyingpankui) {
        User loginUser= (User) session.getAttribute("loginUser");
     //   pkykMapper.update(Panyingpankui.STATE_CHECK,LocalDateTime.now(),id,loginUser.getId());*/


        //库存变动记录表
        storeOper.setGoodId(panyingpankui.getGoodsId());
        System.out.println(panyingpankui.getGoodsId());
        storeOper.setStoreId(panyingpankui.getStoreId());
        storeOper.setNumber(panyingpankui.getNum());
        storeOper.setOperateId(loginUser.getId());
        storeOper.setOperateTime(LocalDateTime.now());
        storeOper.setType(panyingpankui.getType());
        storeOperMapper.insert(storeOper);
       pkykMapper.update(state,checktime,id,shenhe,panyingpankui);
    }
}
